var time = "";
var initialDate = "";
//获得当前的时间
function currentTime() {

    if (initialDate == "") {
        var nowDate = new Date(getInitialDate());
    } else {
        initialDate += 1000;
        var nowDate = new Date(initialDate);
    }

    var hour = changeNum(nowDate.getHours());
    var miunte = changeNum(nowDate.getMinutes());
    var second = changeNum(nowDate.getSeconds());

    return hour + ":" + miunte + ":" + second;
}
//获得当前的日期
function currentDate() {

    var nowDate = new Date(getInitialDate());
    var year = nowDate.getFullYear();
    var month = changeNum(nowDate.getMonth() + 1);
    var date = changeNum(nowDate.getDate());

    return month + "月" + date + "日";
}

function changeNum(t) {
    return t < 10 ? "0" + t : t;
}
$.fn.showCurrentTime = function() {
    var div = $(this);
    return this.each(function() {
        time = currentTime();
        div.text(time);
        setInterval(function() {
            time = currentTime();
            div.text(time);
        }, 1000);
    });

};
$.fn.showCurrentDate = function() {
    var div = $(this);
    return this.each(function() {
        time = currentDate();
        div.text(time);
    });

};

function getInitialDate() {
    $.ajax({
        url: ajaxPrefix()+"/servertime",
        type: "get",
        async: false,
        contentType: "application/json",
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("SessionId", localStorage.getItem('sessionId'));
        },
        success: function(result) {
            initialDate = result.result;
        },
        error: function(result) {}
    });
    return initialDate;
}